
using System;
using System.Windows.Forms;
using RSSRadio.Model;

namespace RSSRadio
{
	public partial class FeedUrlInputDlg : Form
	{
		SubscriptionListView SubscriptionView;
        Subscription feed = null;
        bool saveOnClose = true;

		public FeedUrlInputDlg(SubscriptionListView subscriptionView)
		{
			InitializeComponent();
			SubscriptionView = subscriptionView;


		}
        public FeedUrlInputDlg(SubscriptionListView subscriptionView, Subscription feed)
        {
            InitializeComponent();
            SubscriptionView = subscriptionView;
            this.feed = feed;
            this.textBox1.Text = feed.URL;
            this.RetentionDays.Value = feed.RetnetionDays;
            this.MnuAdd.Text = "Save";


        }

		private void PasteUrl()
		{
			try
			{
				IDataObject contents = Clipboard.GetDataObject();
				string url = (string)contents.GetData(typeof(string));
				if (url != null && url != "")
					textBox1.Text = url;
			}
			catch
			{ }
		}

		private void MnuPaste_Click(object sender, EventArgs e)
		{
			PasteUrl();
		}


        private bool SaveEdits()
        {
            bool result = false;
            if (textBox1.Text.Length != 0)
            {
                try
                {
                    SubscriptionManager feeds = Globals.getSubscriptionManager();

                    Uri feedUri = new Uri(textBox1.Text);
                    if (feed == null)
                    {
                        //Add new
                        feeds.AddSubscription(feedUri.AbsoluteUri, (int)RetentionDays.Value);
                    }
                    else
                    {
                        bool changedRetentionOnly = false;
                        //Update
                        if (feed.RetnetionDays != (int)RetentionDays.Value)
                        {
                            changedRetentionOnly = true;
                            feed.RetnetionDays = (int)RetentionDays.Value;
                        }
                        if (feed.URL != feedUri.AbsoluteUri)
                        {
                            feeds.setSubscriptionURL(feed, feedUri.AbsoluteUri);
                            changedRetentionOnly = false;
                        }
                        if (changedRetentionOnly)
                        {
                            feeds.StoreSubscriptions();
                        }
                    }

                    this.SubscriptionView.LoadSubscriptions();
                    //this.Close();
                    result = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Feed error: " + ex.Message);
                }


            }
            return result;
        }
        private void MnuAdd_Click(object sender, EventArgs e)
        {
            if (SaveEdits())
            {
                saveOnClose = false;
                this.Close();
            }
        }

        private void FeedUrlInputDlg_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (saveOnClose)
            {
                if (!SaveEdits())
                {
                    e.Cancel = true;
                }
            }
        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            saveOnClose = false;
            this.Close();
        }
	}
}